/*
 * CarbonBasedAblationMaterial.h
 * to compute carbonbased material ablation
 *
 *  Created on: Dec 24, 2019
 *      Author: liuxiao
 */

#pragma once

#include "Material.h"
#include "LinearInterpolation.h"

class Function;

class CarbonBasedAblationMaterial : public Material
{
public:
	CarbonBasedAblationMaterial(const InputParameters & parameters);

protected:

	//wall pressure
	MaterialProperty<Real> & _Pw_C1;
	MaterialProperty<Real> & _Pw_C2;
	MaterialProperty<Real> & _Pw_C3;
	MaterialProperty<Real> & _Pw_C4;
	MaterialProperty<Real> & _Pw_C5;
	MaterialProperty<Real> & _Pw_O2;
	MaterialProperty<Real> & _Pw_O;
	MaterialProperty<Real> & _Pw_CO;
	MaterialProperty<Real> & _Pw_CO2;
	MaterialProperty<Real> & _Pw_N2;
	MaterialProperty<Real> & _Pw_N;
	MaterialProperty<Real> & _Pw_CN;
	MaterialProperty<Real> & _Pw_C2N;
	//wall mass concentration
	MaterialProperty<Real> & _Cw_C1;
	MaterialProperty<Real> & _Cw_C2;
	MaterialProperty<Real> & _Cw_C3;
	MaterialProperty<Real> & _Cw_C4;
	MaterialProperty<Real> & _Cw_C5;
	MaterialProperty<Real> & _Cw_O2;
	MaterialProperty<Real> & _Cw_O;
	MaterialProperty<Real> & _Cw_CO;
	MaterialProperty<Real> & _Cw_CO2;
	MaterialProperty<Real> & _Cw_N2;
	MaterialProperty<Real> & _Cw_N;
	MaterialProperty<Real> & _Cw_CN;
	MaterialProperty<Real> & _Cw_C2N;
	//	wall mixture average molar mass
	MaterialProperty<Real> & _M_bar;
	//  dimensionless mass loss rate
	MaterialProperty<Real> & _Bc;
	MaterialProperty<Real> & _Bp;
	MaterialProperty<Real> & _Bw;
	//mass loss rate
	MaterialProperty<Real> & _mc;
	//	mass injection cofficient
	MaterialProperty<Real> & _injection_coff;
	//  flow field params
	Real  _qc;
	Real  _hr;
	Real  _Pe;
	Real  _Pe_O2;
	Real  _Pe_N2;
	Real  _M_e;
	Real  _M_p;
	Real  _mp;
	const VariableValue & _Tw;
	int  _gamma;
	//	choose scala model,=0,slow reaction;=1,fast reaction
	int _scala_model;
	//	equilibrium constants
	Real  _Kp_C1;
	Real  _Kp_C2;
	Real  _Kp_C3;
	Real  _Kp_C4;
	Real  _Kp_C5;
	Real  _Kp_O;
	Real  _Kp_CO2;
	Real  _Kp_CN;
	Real  _Kp_C2N;
	Real  _Kp_N;
	//	molar mass
	Real  _M_C;
	Real  _M_C1;
	Real  _M_C2;
	Real  _M_C3;
	Real  _M_C4;
	Real  _M_C5;
	Real  _M_O2;
	Real  _M_O;
	Real  _M_CO;
	Real  _M_CO2;
	Real  _M_N2;
	Real  _M_N;
	Real  _M_CN;
	Real  _M_C2N;

	//	universal gas constant
	Real _R;
    //



	//functions
	//compute equilibrium constants
	void compute_Kp(Real T);
	//compute  mass concentration with given pressure
	Real compute_Ci_from_P(Real P,Real Mi,Real M_bar);
	//compute pressure of gaseous Carbon
	Real compute_P_Ci(int i,Real Bw,Real phi,Real M_bar,Real T);
	//compute pressure of O2
	Real compute_Pw_O2(Real Pw_CO,Real Pw_CO2,Real Pw_O,Real Bw,Real phi,Real M_bar,Real T);
	//compute pressure of O
	Real compute_Pw_O(Real Pw_O2);
	//compute pressure of CO
	Real compute_Pw_CO(Real Pw_O2,Real Pw_O,Real M_bar,Real Bw);
	//compute pressure of CO2
	Real compute_Pw_CO2(Real Pw_CO,Real Pw_O2);
	//	compute pressure of N2
	Real compute_Pw_N2(Real Bw,Real phi,Real M_bar);
	//	compute pressure of N
	Real compute_Pw_N(Real Pw_N2);
	//	compute pressure of CN
	Real compute_Pw_CN(Real Pw_N2);
	//	compute pressure of N
	Real compute_Pw_C2N(Real Pw_N2);
//	compute wall mixture average molar mass
	Real compute_M_bar(Real Cw_C1,Real Cw_C2,Real Cw_C3,Real Cw_C4,Real Cw_C5,Real Cw_O2,Real Cw_O,Real Cw_CO,Real Cw_CO2,Real Cw_N2,Real Cw_N,Real Cw_CN,Real Cw_C2N);
//compute concentration of Carbon
	Real compute_carbon_w(Real Cw_C1,Real Cw_C2,Real Cw_C3,Real Cw_C4,Real Cw_C5,Real Cw_CO,Real Cw_CO2,Real Cw_CN,Real Cw_C2N);
//	compute Bc
	Real compute_Bc(Real carbon_w,Real Bp);
	//	compute Bp
	Real compute_Bp(Real mp,Real phi);
	//compute mc
		Real compute_mc(Real Bc,Real phi);
	//compute phi
	Real compute_phi(Real Bc,Real M_bar,Real mc,Real mp,int gamma,Real T);
//	iteration for solution of Bw,phi,M_bar,and Cw_i;
	void iteration(Real T,int qp);
//compute properties
	virtual void computeProperties();




};

template<>
InputParameters validParams<CarbonBasedAblationMaterial>();
